package cn.kai.ums.service.impl;

import cn.kai.ums.mapper.UmsPublicCourseMapper;
import cn.kai.ums.model.UmsPublicCourse;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kai.ums.model.UmsGrade;
import cn.kai.ums.service.UmsGradeService;
import cn.kai.ums.mapper.UmsGradeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class UmsGradeServiceImpl extends ServiceImpl<UmsGradeMapper, UmsGrade>
    implements UmsGradeService{

    @Autowired
    private UmsPublicCourseMapper umsPublicCourseMapper;

    @Autowired
    private UmsGradeMapper umsGradeMapper;


    @Override
    public UmsGrade getOnePub(String stuid, String couid) {
        UmsPublicCourse umsPublicCourse = umsPublicCourseMapper.getOnePublic(couid);
        UmsGrade umsGrade1 = umsGradeMapper.getOneGradeBysidcid(stuid,couid);
        if(umsGrade1!=null){
            return null;
        }else {
            UmsGrade umsGrade = new UmsGrade();
            if(umsPublicCourse==null){
                return null;
            }else {
                if(umsPublicCourse.getPublicCourseNum()!=0){
                    Integer num = umsPublicCourse.getPublicCourseNum()-1;
                    umsPublicCourse.setPublicCourseNum(num);
                    umsPublicCourseMapper.updateById(umsPublicCourse);
                    if(couid.length()<12){
                        umsGrade.setGradeSelect(1);
                    }
                    umsGrade.setGradeCourseNo(couid);
                    umsGrade.setGradeStudentNo(stuid);
                    return umsGrade;
                }else {
                    return null;
                }
            }
        }


    }

    @Override
    public List<UmsGrade> studentGrade(String stuid) {

        return umsGradeMapper.studentGrade(stuid);
    }

    @Override
    public List<UmsGrade> studentPubGrade(String stuid) {
        return umsGradeMapper.studentPubGrade(stuid);
    }
}




